/*
2021-8-4
https://www.acwing.com/problem/content/109/
*/
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;
typedef long long ll;
const int N=500010;

ll q[N],s[N];

ll m_sort(int l,int r)
{
    if(l==r) return 0;
    int mid=l+r>>1;
    ll res=m_sort(l,mid)+m_sort(mid+1,r);
    int i=l,j=mid+1,k=0;
    while(i<=mid && j<=r)
    {
        if(q[i]<=q[j]) s[k++]=q[i++];
        else
        {
            res+=mid-i+1;
            s[k++]=q[j++];
        }
    }
    
    while(i<=mid) s[k++]=q[i++];
    while(j<=r) s[k++]=q[j++];
    
    for(i=l,j=0;i<=r;i++,j++) q[i]=s[j];
    
    return res;
}

int main()
{
    int n;
    while(cin>>n,n)
    {
        for(int i=0;i<n;i++) cin>>q[i];
        
        ll res=m_sort(0,n-1);
        cout<<res<<endl;
    }
    return 0;
}

